<template>
  <div class="custom-food-manager-container">
    <h3 class="manager-title">自定义食物</h3>
    
    <!-- 添加食物表单 -->
    <div class="add-food-form">
      <div class="form-row">
        <input
          type="text"
          v-model="newFoodName"
          placeholder="输入食物名称"
          class="food-name-input"
          @keyup.enter="addFood"
        />
        <select v-model="newFoodCategory" class="category-select">
          <option value="chinese">中餐</option>
          <option value="western">西餐</option>
          <option value="japanese">日料</option>
          <option value="korean">韩餐</option>
          <option value="fastfood">快餐</option>
          <option value="snack">小吃</option>
          <option value="dessert">甜品</option>
          <option value="drink">饮品</option>
        </select>
        <button class="add-button" @click="addFood" :disabled="!newFoodName.trim()">
          添加
        </button>
      </div>
    </div>
    
    <!-- 自定义食物列表 -->
    <div class="custom-foods-list" v-if="customFoods.length > 0">
      <div 
        v-for="food in customFoods" 
        :key="food.id"
        class="custom-food-item"
      >
        <div class="food-info">
          <span class="food-name">{{ food.name }}</span>
          <span class="food-category">{{ getCategoryName(food.category) }}</span>
        </div>
        <button class="delete-button" @click="deleteFood(food.id)">
          <svg class="delete-icon" viewBox="0 0 24 24" width="16" height="16">
            <path fill="currentColor" d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"></path>
          </svg>
        </button>
      </div>
    </div>
    
    <div v-else class="no-custom-foods">
      <p>暂无自定义食物</p>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CustomFoodManager',
  props: {
    customFoods: {
      type: Array,
      default: () => []
    }
  },
  emits: ['add-food', 'delete-food'],
  data() {
    return {
      newFoodName: '',
      newFoodCategory: 'chinese'
    };
  },
  methods: {
    getCategoryName(category) {
      const categoryMap = {
        'chinese': '中餐',
        'western': '西餐',
        'japanese': '日料',
        'korean': '韩餐',
        'fastfood': '快餐',
        'snack': '小吃',
        'dessert': '甜品',
        'drink': '饮品'
      };
      return categoryMap[category] || category;
    },
    addFood() {
      const trimmedName = this.newFoodName.trim();
      if (!trimmedName) {
        return;
      }
      
      const newFood = {
        id: Date.now(), // 使用时间戳作为唯一ID
        name: trimmedName,
        category: this.newFoodCategory,
        isCustom: true
      };
      
      this.$emit('add-food', newFood);
      
      // 重置表单
      this.newFoodName = '';
      this.newFoodCategory = 'chinese';
    },
    deleteFood(id) {
      if (confirm('确定要删除这个食物吗？')) {
        this.$emit('delete-food', id);
      }
    }
  }
}
</script>

<style scoped>
.custom-food-manager-container {
  background: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.manager-title {
  margin: 0 0 16px 0;
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.add-food-form {
  margin-bottom: 20px;
}

.form-row {
  display: flex;
  gap: 10px;
  align-items: center;
}

.food-name-input {
  flex: 1;
  padding: 10px 14px;
  border: 2px solid #e9ecef;
  border-radius: 8px;
  font-size: 14px;
  transition: border-color 0.2s ease;
}

.food-name-input:focus {
  outline: none;
  border-color: #409eff;
}

.category-select {
  padding: 10px 14px;
  border: 2px solid #e9ecef;
  border-radius: 8px;
  font-size: 14px;
  background: white;
  cursor: pointer;
  transition: border-color 0.2s ease;
}

.category-select:focus {
  outline: none;
  border-color: #409eff;
}

.add-button {
  padding: 10px 20px;
  background: #409eff;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.add-button:hover:not(:disabled) {
  background: #66b1ff;
  transform: translateY(-1px);
}

.add-button:disabled {
  background: #c0c4cc;
  cursor: not-allowed;
}

.custom-foods-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.custom-food-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px;
  background: #f8f9fa;
  border-radius: 8px;
  transition: all 0.2s ease;
}

.custom-food-item:hover {
  background: #e9ecef;
  transform: translateX(4px);
}

.food-info {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
}

.food-name {
  font-weight: 500;
  font-size: 14px;
  color: #333;
}

.food-category {
  font-size: 12px;
  padding: 2px 8px;
  background: rgba(0, 0, 0, 0.08);
  border-radius: 10px;
  color: #666;
}

.delete-button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  background: #ff4d4f;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.delete-button:hover {
  background: #ff7875;
  transform: scale(1.05);
}

.delete-icon {
  width: 16px;
  height: 16px;
}

.no-custom-foods {
  text-align: center;
  padding: 20px 0;
  color: #999;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .custom-food-manager-container {
    padding: 16px;
  }
  
  .manager-title {
    font-size: 15px;
    margin-bottom: 12px;
  }
  
  .form-row {
    flex-direction: column;
    align-items: stretch;
  }
  
  .food-name-input,
  .category-select,
  .add-button {
    width: 100%;
  }
  
  .add-button {
    padding: 10px;
  }
  
  .custom-food-item {
    padding: 10px;
  }
  
  .food-name {
    font-size: 13px;
  }
}

@media (max-width: 480px) {
  .custom-food-manager-container {
    padding: 14px;
  }
  
  .manager-title {
    font-size: 14px;
  }
  
  .custom-food-item {
    padding: 8px;
  }
  
  .food-info {
    gap: 8px;
  }
  
  .delete-button {
    width: 28px;
    height: 28px;
  }
}
</style>